#include "recboxitem.h"
#include "ui_recboxitem.h"
#include <QPropertyAnimation>
#include <QDebug>

recBoxItem::recBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::recBoxItem)
{
    ui->setupUi(this);
    ui->musicImageBox->installEventFilter(this);
}

recBoxItem::~recBoxItem()
{
    delete ui;
}

bool recBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    // 注意：recItem上有⼀个按钮，当⿏标放在按钮上时在开启动画
    if(watched == ui->musicImageBox)
    {
        int ImgWidget = ui->musicImageBox->width();
        int ImgHeight = ui->musicImageBox->height();
        // 拦截⿏标进⼊事件
        if(event->type() == QEvent::Enter)
            {
                QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry");
                animation->setDuration(100);
                animation->setStartValue(QRect(9,10,ImgWidget,ImgHeight));
                animation->setEndValue(QRect(9, 0, ImgWidget, ImgHeight));
                animation->start();
                // 注意：动画结束的时候会触发finished信号，拦截到该信号，销毁animation
                connect(animation, &QPropertyAnimation::finished, this, [=](){
                    delete animation;
                    qDebug()<<"图⽚上移动画结束";
                });
                return true;

            }
        else if(event->type() == QEvent::Leave)
            {
                // 拦截⿏标离开事件
                QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry");
                animation->setDuration(150);
                animation->setStartValue(QRect(9,0,ImgWidget,ImgHeight));
                animation->setEndValue(QRect(9, 10, ImgWidget, ImgHeight));
                animation->start();
                // 注意：动画结束的时候会触发finished信号，拦截到该信号，销毁animation
                connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
                qDebug()<<"图⽚上移动画结束";
                });
                return true;
            }
       }
    return QObject::eventFilter(watched, event);
}

void recBoxItem::setText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void recBoxItem::setImage(const QString &Imagepath)
{
    QString imgStyle = "border-image:url("+Imagepath+");";
    ui->RecMusicImage->setStyleSheet(imgStyle);
}
